package JAVA300.第八章常用类;
/*
测试时间日历
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class TestCalender {
    public static void main(String[] args) throws ParseException{
        //获得相关元素
        Calendar calendar = new GregorianCalendar(29999,10,9,22,10,50);
        int year = calendar.get(Calendar.YEAR);
        int weekDay = calendar.get(Calendar.SATURDAY);
        int date = calendar.get(Calendar.DATE);
        System.out.println(date);

        //设置日期
        Calendar c2 = new GregorianCalendar();//没有设置参数 默认当前日期
        c2.set(Calendar.YEAR,1997);
        System.out.println(c2);

        //日期计算
        Calendar c3 = new GregorianCalendar();
        c3.add(Calendar.YEAR,-23);
        System.out.println(c3);

        //日期打印
        PrintDate(calendar);
        //可视化日历
        Calender1();
    }
//日期打印
    public static void PrintDate(Calendar c){
        //print date formate :1997-9-3 12:13:11 week7
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;//0-Jan
        int date = c.get(Calendar.DAY_OF_MONTH);

        int hour = c.get(Calendar.HOUR);
        int min = c.get(Calendar.MINUTE);
        int sec = c.get(Calendar.SECOND);

        int week = c.get(Calendar.DAY_OF_WEEK)-1;//1-Sunday
        String weekDay = week==0?7+"":week+"";

        System.out.println(year+"-"+month+"-"+date+" "+hour+":"+min+":"+sec+" "+"week"+weekDay);


    }
    //可视化日历--给一个日期 输出其当月日历
    public static void Calender1() throws ParseException {
        //str转成日历对象
        //String str = "2020-6-7";
        System.out.println("请输入日期：");
        Scanner scanner = new Scanner(System.in);//获取键盘输入
        String str = scanner.nextLine();

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = df.parse(str);
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        System.out.println(c);
//表头输出
        System.out.println("日\t一\t二\t三\t四\t五\t六");


        int nowDay = c.get(Calendar.DAY_OF_MONTH);//保存当前日期
        c.set(Calendar.DAY_OF_MONTH,1);//打印输出日期 从1开始

        for (int i=0;i<c.get(Calendar.DAY_OF_WEEK)-1;i++){//用于首行输出正确的对应周数
            System.out.print("\t");
        }

        int alldate = c.getActualMaximum(Calendar.DATE);//本月总天数
        for(int i=1;i<=alldate;i++){
            if (c.get(Calendar.DAY_OF_MONTH) == nowDay){
                System.out.print(c.get(Calendar.DAY_OF_MONTH)+"*\t");
            }else {
                System.out.print(c.get(Calendar.DAY_OF_MONTH)+"\t");
            }




            if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
                System.out.println();//换行
            }
            c.add(Calendar.DAY_OF_MONTH,1);//日期加一 否则 日期就不变了
        }


    }

}
